package psn.kiko.config.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 请求记录过滤器，仅通过日志记录请求
 * 2024/12/2 22:19
 */
@Component
@Slf4j
@Order(-1)
public class RequestRecordFilter implements Filter{
	@Override
	public void init(FilterConfig conf){
		log.debug("initialized filter {}",conf.getFilterName());
	}
	
	@Override
	public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain) throws ServletException, IOException{
		if(req instanceof HttpServletRequest){
			HttpServletRequest httpReq = (HttpServletRequest) req;
			log.debug("filtering: {}:{}_{}->{}",httpReq.getRemoteHost(),httpReq.getRemotePort(),httpReq.getMethod(),
					httpReq.getRequestURI());
		}
		chain.doFilter(req, resp);
	}
}
